﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Zero.UControl.Demo.Views
{
    /// <summary>
    /// TriangleLoading1.xaml 的交互逻辑
    /// </summary>
    public partial class TriangleLoading1 : UserControl
    {
        public TriangleLoading1()
        {
            InitializeComponent();
            StartProgressAnimation();
            Task.Run(() =>
            {
                while (true)
                {
                    Thread.Sleep(500);
                    ProgressText.Dispatcher.Invoke(() =>
                    {
                        Current++;
                        if (Current > 100)
                            Current = 0;
                    });
                }
            });
        }

        private int current = 0;

        public int Current
        {
            get { return current; }
            set 
            { 
                current = value;
                ProgressText.Text = current.ToString() + "%";
            }
        }


        private void StartProgressAnimation()
        {
            ProgressPath.StrokeDashArray = new DoubleCollection { 3, 3 };

            double trianglePerimeter = 6;
            // 从 0 增加到完整三角形长度
            DoubleAnimation progressAnimation = new DoubleAnimation
            {
                From = 0, 
                To = trianglePerimeter,
                Duration = new Duration(TimeSpan.FromSeconds(1)), // 动画持续
                RepeatBehavior = RepeatBehavior.Forever // 循环
            };
            ProgressPath.BeginAnimation(System.Windows.Shapes.Path.StrokeDashOffsetProperty, progressAnimation);
        }
    }
}
